package com.myway.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.myway.dto.ibe_param.TicketPrice;
import com.myway.utils.TokenProccessor;
import com.myway.vo.*;
import com.myway.service.CountPriceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

//import com.myway.front.ibe_param.TicketPrice;
//import com.myway.front.service.CountPriceService;

/**
 * Created by wsq on 2018/8/4.
 */
@Controller
@RequestMapping("/en/confirmation")
public class ConfirmationController {
    @Autowired
    private CountPriceService countPriceService;

    @RequestMapping(value = "/getConfirmationInfo")
    @ResponseBody
    public ModelAndView getConfirmationInfo(HttpServletRequest request, @RequestBody String confimationInfoVal) {
        System.out.println(confimationInfoVal);
        String token = TokenProccessor.getInstance().makeToken();//创建令牌
        System.out.println("在FormServlet中生成的token："+token);
        request.getSession().setAttribute("token", token);  //在服务器使用session保存token(令牌)

        confimationInfoVal = confimationInfoVal.replaceAll("confimationInfoVal=", "");
        Map<String, Object> map = JSON.parseObject(confimationInfoVal, Map.class);
//        List<FlyPassengeVo> passgaes = (List<FlyPassengeVo>) map.get("passenges");
        List<FlyPassengeVo> passgaes =JSONArray.parseArray(map.get("passenges").toString(),FlyPassengeVo.class);
        List<FlyPassengeVo> adultPassages=new ArrayList<FlyPassengeVo>();
        List<FlyPassengeVo> noAdultPassages=new ArrayList<FlyPassengeVo>();
        for (int i = 0; i <passgaes.size() ; i++) {
            FlyPassengeVo passengeVo = passgaes.get(i);
            if(passengeVo.getPassageType().equals("1")){
                adultPassages.add(passengeVo);
                continue;
            }
            noAdultPassages.add(passengeVo);
        }
        for (int i = 0; i <adultPassages.size() ; i++) {
            FlyPassengeVo adultPassengeVo = adultPassages.get(i);
            List<FlyPassengeVo>  accompanyingPassengerList=new ArrayList<FlyPassengeVo>();
            for (int j = 0; j <noAdultPassages.size() ; j++) {
                FlyPassengeVo noAdultPassengeVo = noAdultPassages.get(j);
                if(adultPassengeVo.getAdtIndex().equals(noAdultPassengeVo.getAdtIndex())){
                    accompanyingPassengerList.add(noAdultPassengeVo);
                }
            }
            adultPassengeVo.setAccompanyingPassengerList(accompanyingPassengerList);
        }

        System.out.println(passgaes);
        List<FlySegmentVo> segmentVos = JSONArray.parseArray(map.get("segments").toString(),FlySegmentVo.class);
//        (List<FlySegmentVo>) map.get("segments");
        ModelAndView mv = new ModelAndView();
        request.getSession().setAttribute("adultPassages", adultPassages);  //在服务器使用session保存adultPassages乘客信息
        mv.addObject("passengers", passgaes);
        mv.addObject("contactMan", map.get("contactMan"));
        mv.addObject("segments", segmentVos);

        List<TicketPrice> ticketPrices= (List<TicketPrice>) request.getSession().getAttribute("priceSession");
        Map priceMap=countPriceService.countCabinTypePrice(ticketPrices);
        mv.addObject("cabinTypePrice",priceMap.get("cabinTypePrice"));//税费
        mv.addObject("totalFlyPrice",priceMap.get("totalFlyPrice"));//机票价


        mv.setViewName("/en/confirmation");
        return mv;
    }

}